package com.ftg.learn.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.mall.dto.pmsProductAttributeParam;
import com.ftg.learn.mall.entity.PmsProductAttribute;
import com.ftg.learn.mall.entity.PmsProductAttributeCategory;
import com.ftg.learn.mall.mapper.PmsProductAttributeCategoryMapper;
import com.ftg.learn.mall.mapper.PmsProductAttributeMapper;
import com.ftg.learn.mall.service.IPmsProductAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@Service
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements IPmsProductAttributeService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    PmsProductAttributeMapper attributeMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    PmsProductAttributeCategoryMapper categoryMapper;


    @Override
    public List<pmsProductAttributeParam> findAllAndPage(int type, Page<pmsProductAttributeParam> page) {
        return attributeMapper.findAllAndPage(type, page);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public boolean updateAttributeCount(List<Long> ids) {
        for (Long id : ids) {
            Long productAttributeCategoryId = attributeMapper.selectProductAttributeCategoryId(id);
            this.removeById(id);
            attributeMapper.updateAttributeCount(productAttributeCategoryId);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public boolean updateParamCount(List<Long> ids) {
        for (Long id : ids) {
            Long productAttributeCategoryId = attributeMapper.selectProductAttributeCategoryId(id);
            this.removeById(id);
            attributeMapper.updateParamCount(productAttributeCategoryId);
        }
        return true;
    }

    /**
     * 添加
     *
     * @param pmsProductAttribute
     * @return
     */
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean insertattribute(PmsProductAttribute pmsProductAttribute) {
        this.save(pmsProductAttribute);
        Long id = attributeMapper.findid(pmsProductAttribute.getProductAttributeId());
        attributeMapper.updatettributeCount(id);
        return true;
    }

    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean insertparam(PmsProductAttribute pmsProductAttribute) {
        this.save(pmsProductAttribute);
        Long id = attributeMapper.findid(pmsProductAttribute.getProductAttributeId());
        attributeMapper.updateparam(id);
        return true;
    }

    /**
     * 删除
     */

    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean delattribute(Long id) {
        Long cid=attributeMapper.findid(id);
        attributeMapper.delattribute(cid);
        this.removeById(id);
        return true;
    }

    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean delparam(Long id) {
        Long cid=attributeMapper.findid(id);
        attributeMapper.delparam(cid);
        this.removeById(id);
        return true;
    }
    /**
     *修改
     */
    @Override
    public boolean updateattribule(PmsProductAttribute pmsProductAttribute) {
        this.updateById(pmsProductAttribute);
        return true;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public boolean deleteAndUpdateCount(List<Long> ids) {
        PmsProductAttribute productAttribute = attributeMapper.findPmsProductAttribute(ids.get(0));
        PmsProductAttributeCategory pmsProductAttributeCategory = attributeMapper.findPmsProductAttributeCategory(productAttribute.getProductAttributeCategoryId());
        int count = ids.size();
        Integer type = productAttribute.getType();
        if (type == 0) {
            if (pmsProductAttributeCategory.getAttributeCount() >= count) {
                pmsProductAttributeCategory.setAttributeCount(pmsProductAttributeCategory.getAttributeCount() - count);
            } else {
                pmsProductAttributeCategory.setAttributeCount(0);
            }
        } else if (type == 1) {
            if (pmsProductAttributeCategory.getParamCount() >= count) {
                pmsProductAttributeCategory.setParamCount(pmsProductAttributeCategory.getParamCount() - count);
            } else {
                pmsProductAttributeCategory.setParamCount(0);
            }
        }
        categoryMapper.updateById(pmsProductAttributeCategory);
        this.removeByIds(ids);
        return true;
    }


    @Override
    public boolean insertAndUpdateCount(pmsProductAttributeParam pmsProductAttributeParam) {
        PmsProductAttribute pmsProductAttribute = new PmsProductAttribute();
        BeanUtils.copyProperties(pmsProductAttributeParam, pmsProductAttribute);
        attributeMapper.insert(pmsProductAttribute);
        //新增商品属性以后需要更新商品属性分类数量
        PmsProductAttributeCategory pmsProductAttributeCategory = attributeMapper.findPmsProductAttributeCategory(pmsProductAttribute.getProductAttributeCategoryId());
        if (pmsProductAttribute.getType() == 0) {
            pmsProductAttributeCategory.setAttributeCount(pmsProductAttributeCategory.getAttributeCount() + 1);
        } else if (pmsProductAttribute.getType() == 1) {
            pmsProductAttributeCategory.setParamCount(pmsProductAttributeCategory.getParamCount() + 1);
        }
        categoryMapper.updateById(pmsProductAttributeCategory);
        return true;
    }

    @Override
    public boolean delAndUpdateCount(Long id) {
        PmsProductAttribute productAttribute = attributeMapper.findPmsProductAttribute(id);
        PmsProductAttributeCategory pmsProductAttributeCategory = attributeMapper.findPmsProductAttributeCategory(productAttribute.getProductAttributeCategoryId());
        Integer type = productAttribute.getType();
        if (type == 0) {
            pmsProductAttributeCategory.setAttributeCount(pmsProductAttributeCategory.getAttributeCount() - 1);
        } else if (type == 1) {
            pmsProductAttributeCategory.setParamCount(pmsProductAttributeCategory.getParamCount() - 1);

        }
        categoryMapper.updateById(pmsProductAttributeCategory);
        this.removeById(id);
        return true;
    }


}
